/**
 * @file support.c
 * @brief 
 * @author 2bam 
 * @version 1.0
 * @date 2023-08-20
 * 
 * @copyright Copyright (c) 2023  NULL
 * 
 * @change log:
 * <table>
 * <tr><th>Date       <th>Version <th>Author  <th>Description
 * <tr><td>2023-08-20 <td>1.0     <td>2bam     <td>内容
 * </table>
 */


/* Includes -------------------------------------------------------------------*/
#include "support.h"
#include <rtthread.h>
#include <stddef.h>


#define DBG_TAG "support"
#define DBG_LVL DBG_LOG
#include <rtdbg.h>
/* Private define -------------------------------------------------------------*/
#define EVENT_FLAG_1    (0x1<<0)
#define EVENT_FLAG_2    (0x1<<2)
#define EVENT_FLAG_3    (0x1<<3)

/* Private typedef ------------------------------------------------------------*/

/* Private variables ----------------------------------------------------------*/
static rt_thread_t thread1 = RT_NULL;          //线程1的句柄
static rt_thread_t thread2 = RT_NULL;          //线程2的句柄
static rt_thread_t thread3 = RT_NULL;          //线程3的句柄
static rt_sem_t semaphore1,semaphore2;          //semaphore
static rt_mutex_t mutex1 = RT_NULL;                   //mutex
//static uint32_t flag = 0;
static rt_event_t event1;
/* Private function prototypes ------------------------------------------------*/
static void user_task1_entry(void *parameter);
static void user_task2_entry(void *parameter);
static void user_task3_entry(void *parameter);

/**
 * @brief 
 * @param  parameterMy Param doc
 * @date 2023-08-20
 * @author 2bam 
 */
static void user_task1_entry(void *parameter)
{
    extern void led_red_toggle(void);
    extern void led_green_toggle(void);

    while(1)
    {
        led_red_toggle();
        led_green_toggle();
        rt_thread_delay(1000);
    }
}

/**
 * @brief 
 * @param  parameter   My Param doc
 * @date 2023-08-23
 * @author 2bam 
 */
static void user_task2_entry(void *parameter)
{
    while(1)
    {
//        rt_event_recv(event1,EVENT_FLAG_2,RT_EVENT_FLAG_AND|RT_EVENT_FLAG_CLEAR,RT_WAITING_FOREVER,NULL);
//        rt_kprintf("task2 running.\n");
//        rt_event_send(event1,EVENT_FLAG_3);
        rt_thread_delay(2000);
    }
}

/**
 * @brief 
 * @param  parameter   My Param doc
 * @date 2023-08-24
 * @author 2bam 
 */
static void user_task3_entry(void *parameter)
{
    while(1)
    {
        //rt_kprintf("user task2 running...\n");
        //rt_thread_mdelay(ms);
        // rt_sem_take(semaphore2,RT_WAITING_FOREVER);
        // if(flag>0)
        //     flag--;
        // rt_sem_release(semaphore1);

//        rt_event_recv(event1,EVENT_FLAG_3,RT_EVENT_FLAG_AND|RT_EVENT_FLAG_CLEAR,RT_WAITING_FOREVER,NULL);
//        rt_kprintf("task3 running.\n");
//        rt_event_send(event1,EVENT_FLAG_1);
        rt_thread_delay(2000);
    }
}

/**
 * @brief 
 * @return int 
 * @date 2023-08-20
 * @author 2bam 
 */
int user_task1_create(void)
{
    /*创建task1*/
    thread1 = rt_thread_create("task1",
                                    user_task1_entry,
                                    NULL,
                                    1024,
                                    15,
                                    1000);
    if(thread1 == RT_NULL)
    {
        LOG_E("task1 create failed!\n");
    }
    else
    {
        LOG_D("task1 create success.\n");
    }

    /* 启动线程 */
    rt_thread_startup(thread1);

    return 0;
}
INIT_APP_EXPORT(user_task1_create);

/**
 * @brief 
 * @return int 
 * @date 2023-08-20
 * @author 2bam 
 */
int user_task2_create(void)
{
    /*创建task2*/
    thread2 = rt_thread_create("task2",
                                    user_task2_entry,
                                    NULL,
                                    1024,
                                    15,
                                    1000);
    if(thread2 == RT_NULL)
    {
        LOG_E("task2 create failed!\n");
    }
    else
    {
        LOG_D("task2 create success.\n");
    }
    /* 启动线程 */
    rt_thread_startup(thread2);
    return 0;
}
INIT_APP_EXPORT(user_task2_create);

/**
 * @brief 
 * @return int 
 * @date 2023-08-24
 * @author 2bam 
 */
int user_task3_create(void)
{
    /*创建task3*/
    thread3 = rt_thread_create("task3",
                                    user_task3_entry,
                                    NULL,
                                    1024,
                                    15,
                                    1000);
    if(thread3 == RT_NULL)
    {
        LOG_E("task3 create failed!\n");
    }
    else
    {
        LOG_D("task3 create success.\n");
    }
    /* 启动线程 */
    rt_thread_startup(thread3);
    return 0;
}
INIT_APP_EXPORT(user_task3_create);


/**
 * @brief 
 * @return int 
 * @date 2023-08-23
 * @author 2bam 
 */
int user_sem_create(void)
{
    semaphore1 = rt_sem_create("semaphore1",1,RT_IPC_FLAG_FIFO);
    if(semaphore1 == RT_NULL)
    {
        LOG_E("semaphore1 create failed!\n");
    }
    else
    {
        LOG_D("semaphore1 create success.\n");
    }

    semaphore2 = rt_sem_create("semaphore2",0,RT_IPC_FLAG_FIFO);
    if(semaphore2 == RT_NULL)
    {
        LOG_E("semaphore2 create failed!\n");
    }
    else
    {
        LOG_D("semaphore2 create success.\n");
    }
    return 0;
}
INIT_APP_EXPORT(user_sem_create);


/**
 * @brief 
 * @return int 
 * @date 2023-08-23
 * @author 2bam 
 */
int user_mutex_create(void)
{
    mutex1 = rt_mutex_create("mutex1",RT_IPC_FLAG_FIFO);
    if(mutex1 == RT_NULL)
    {
        LOG_E("mutex1 create failed!");
    }
    else
    {
        LOG_D("mutex1 create success.");
    }
    return 0;
}
INIT_APP_EXPORT(user_mutex_create);


/**
 * @brief 
 * @return int 
 * @date 2023-08-24
 * @author 2bam 
 */
int user_event_create(void)
{
    event1 = rt_event_create("event1",RT_IPC_FLAG_FIFO);
    if(event1 == RT_NULL)
    {
        LOG_E("event1 create failed!\n");
    }
    else
    {
        LOG_D("event1 create success.\n");
    }
    return 0;
}
INIT_APP_EXPORT(user_event_create);

